package com.enation.app.javashop.api.client;

import com.enation.app.javashop.model.promotion.fulldiscount.vo.FullDiscountVO;
import com.enation.app.javashop.model.promotion.halfprice.vo.HalfPriceVO;
import com.enation.app.javashop.model.promotion.minus.vo.MinusVO;
import com.enation.app.javashop.model.promotion.seckill.dos.SeckillApplyDO;
import com.enation.app.javashop.model.promotion.tool.dos.PromotionGoodsDO;
import com.enation.app.javashop.model.promotion.tool.dto.PromotionDTO;
import com.enation.app.javashop.model.promotion.tool.vo.PromotionVO;
import com.enation.app.javashop.service.promotion.fulldiscount.FullDiscountManager;
import com.enation.app.javashop.service.promotion.groupbuy.GroupbuyGoodsManager;
import com.enation.app.javashop.service.promotion.halfprice.HalfPriceManager;
import com.enation.app.javashop.service.promotion.minus.MinusManager;
import com.enation.app.javashop.service.promotion.seckill.SeckillGoodsManager;
import com.enation.app.javashop.service.promotion.tool.PromotionGoodsManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author zh
 * @version v2.0
 * @Description: 促销商品api
 * @date 2018/8/2111:20
 * @since v7.0.0
 */
@RestController
@RequestMapping("/client/trade/promotion")
@ApiIgnore
public class PromotionGoodsClientController {

    @Autowired
    private PromotionGoodsManager promotionGoodsManager;

    @Autowired
    private SeckillGoodsManager seckillGoodsManager;

    @Autowired
    private GroupbuyGoodsManager groupbuyGoodsManager;

    @Autowired
    private FullDiscountManager fullDiscountManager;

    @Autowired
    private MinusManager minusManager;

    @Autowired
    private HalfPriceManager halfPriceManager;

    @DeleteMapping("/goods")
    public void delPromotionGoods(@RequestParam("goods_id") Long goodsId, @RequestParam("type") String type, @RequestParam("activity_id") Long activityId) {
        promotionGoodsManager.delete(goodsId, activityId, type);
    }

    @RequestMapping(value = "/goods/skuids", method = RequestMethod.DELETE)
    public void delPromotionGoods(@RequestBody List<Long> delSkuIds){

        //删除促销商品
        promotionGoodsManager.delete(delSkuIds);
    }


    /**
     * 根据商品id读取商品参与的所有活动（有效的活动）
     *
     * @param goodsId
     * @return 返回活动的集合
     */
    @RequestMapping(value = "/goods/{goods_id}", method = RequestMethod.GET)
    public List<PromotionVO> getPromotion(@PathVariable("goods_id")Long goodsId){

        return promotionGoodsManager.getPromotion(goodsId);
    }

    @RequestMapping(value = "/seckill/rollback-stock", method = RequestMethod.POST)
    public void rollbackSeckillStock(@RequestBody List<PromotionDTO> promotionDTOList) {
        seckillGoodsManager.rollbackStock(promotionDTOList);
    }

    @RequestMapping(value = "/seckill/add-sold-num", method = RequestMethod.POST)
    public boolean addSeckillSoldNum(@RequestBody List<PromotionDTO> promotionDTOList) {
        return seckillGoodsManager.addSoldNum(promotionDTOList);
    }

    @RequestMapping(value = "/seckill/{seckill_id}/seckill-apply", method = RequestMethod.GET)
    public List<SeckillApplyDO> getSeckillGoodsList(@PathVariable("seckill_id") Long seckillId, @RequestParam("status") String status) {
        return seckillGoodsManager.getGoodsList(seckillId, status);
    }

    @RequestMapping(value = "/groupbuy/{order_sn}/rollback-stock", method = RequestMethod.POST)
    public void rollbackGroupbuyStock(@RequestBody List<PromotionDTO> promotionDTOList, @PathVariable("order_sn")String orderSn) {
        groupbuyGoodsManager.rollbackStock(promotionDTOList,orderSn);
    }

    @RequestMapping(value = "/groupbuy/{order_sn}/cut-stock", method = RequestMethod.POST)
    public boolean cutGroupbuyQuantity(@PathVariable("order_sn")String orderSn, @RequestBody List<PromotionDTO> promotionDTOList) {
        return groupbuyGoodsManager.cutQuantity(orderSn, promotionDTOList);
    }

    @RequestMapping(value = "/groupbuy/goods-info/{goods_ids}", method = RequestMethod.POST)
    public void updateGroupbuyGoodsInfo(@PathVariable("goods_ids") Long[] goodsIds) {
        groupbuyGoodsManager.updateGoodsInfo(goodsIds);
    }

    @RequestMapping(value = "/groupbuy/{order_sn}/add-stock", method = RequestMethod.POST)
    public void addGroupbuyQuantity(@PathVariable("order_sn")String orderSn) {
        groupbuyGoodsManager.addQuantity(orderSn);
    }

    @RequestMapping(value = "/groupbuy/renew-buy-num", method = RequestMethod.POST)
    public boolean renewGroupbuyBuyNum(@RequestParam("goodsid") Long goodid, @RequestParam("num")Integer num, @RequestParam("product_id") Long productId){

        return groupbuyGoodsManager.renewBuyNum(goodid,num,productId);
    }

    @RequestMapping(value = "/activity/{activity_id}/goods", method = RequestMethod.GET)
    public List<PromotionGoodsDO> getPromotionGoods(@PathVariable("activity_id")Long activityId, @RequestParam("promotion_type") String promotionType) {
        return promotionGoodsManager.getPromotionGoods(activityId, promotionType);
    }

    @RequestMapping(value = "/full-discount/{id}", method = RequestMethod.GET)
    public FullDiscountVO getFullDiscountModel(@PathVariable("id")Long fdId) {
        return fullDiscountManager.getModel(fdId);
    }

    @RequestMapping(value = "/minus/{minus_id}", method = RequestMethod.GET)
    public MinusVO getMinusFromDB(@PathVariable("minus_id") Long minusId) {
        return minusManager.getFromDB(minusId);
    }

    @RequestMapping(value = "/half-price/{id}", method = RequestMethod.GET)
    public HalfPriceVO getHalfPriceFromDB(@PathVariable("id")Long id) {
        return halfPriceManager.getFromDB(id);
    }


    @GetMapping("/goods-list")
    public List<PromotionGoodsDO> getPromotionGoodsList(@RequestParam("promotion_type") String promotionType, @RequestParam("now_time")Long nowTime) {
        return promotionGoodsManager.getPromotionGoodsList(promotionType, nowTime);
    }

    @PostMapping("/{pg_id}/end-time")
    public void updatePromotionEndTime(@RequestParam("end_time") Long endTime, @PathVariable("pg_id") Long pgId) {
        promotionGoodsManager.updatePromotionEndTime(endTime, pgId);
    }

    @PutMapping("/exchange-goods-time")
    public void updateExchangeGoodsTime(@RequestParam("goods_id") Long goodsId, @RequestParam("start_time") Long startTime, @RequestParam("end_time") Long endTime){
        promotionGoodsManager.updateExchangeGoodsTime(goodsId, startTime, endTime);
    }

}
